import { fileURLToPath, URL } from "node:url";

import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";
import vueJsx from "@vitejs/plugin-vue-jsx";
import { loadEnv } from "vite";
// 有赞
import Components from "unplugin-vue-components/vite";
import { VantResolver } from "unplugin-vue-components/resolvers";

// https://vitejs.dev/config/
export default ({ mode }) => {
  console.log(loadEnv(mode, process.cwd()).VITE_APP_URI, "VITE_APP_URI");
  return defineConfig({
    plugins: [
      vue(),
      vueJsx(),
      Components({
        resolvers: [VantResolver()],
      }),
    ],
    resolve: {
      alias: {
        "@": fileURLToPath(new URL("./src", import.meta.url)),
      },
    },
    server: {
      assetsPublicPath: "/",
      hmr: true,
      host: "0.0.0.0",
      port: "925",
      proxy: {
        "/api": {
          target: loadEnv(mode, process.cwd()).VITE_APP_URI, //后端接口地址
          ws: true, //接受websocket请求
          changeOrigin: true, //是否允许跨越
          chunkOrigins: true,
          pathRewrite: {
            // "^/api": "api", //重写,
          },
        },
      },
    },
  });
};
